package com.yequan.designpattern.strategy.one;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author yq
 * @date 2020/09/27
 */
public class ExeExecutor {

    private volatile static ExeExecutor executor = null;

    private static Map<String, ExeHandle> exeMap = new HashMap<>();

    private static ExecutorService executorService;

    private ExeExecutor() {
        exeMap.put("bpa-a", new AExeHandler());
        exeMap.put("flow-b", new BExeHandler());
        executorService = Executors.newFixedThreadPool(5);
    }

    public static ExeExecutor getInstance() {
        if (null == executor) {
            synchronized (ExeExecutor.class) {
                if (null == executor) {
                    executor = new ExeExecutor();
                }
            }
        }
        return executor;
    }

    public void execute(ExeVo exeVo) {
        String key = exeVo.getKey();
        if (exeMap.containsKey(key)) {
            ExeHandle exeHandle = exeMap.get(key);
            if (null != exeHandle) {
                executorService.submit(new ExeExecutorTask(exeHandle, exeVo));
            } else {
                System.out.println("不支持的计算类型。。。");
            }
        }else {
            System.out.println("不支持的计算类型。。。");
        }
    }

}
